<?php


namespace app\api\model;

use app\api\controller\Code;
use think\Db;
use think\Model;

/**
 * 会议
 * Class MeetingModel
 * @package app\api\model
 *
 */
class MeetingModel extends Model
{

    /**
     * 设置表名
     * @var string
     */
    protected $table = 'ah_meeting';

    /**
     *
     * 会议列表
     */
    public static function meetingList($id,$type){

        $where = [];//=['exp','FIND_IN_SET(1,expert_ids)'];
        $data = self::where($where)->alias('a');

        if ($type == 0){

            $data->where(['a.user_id'=>$id,'a.type'=>0]);
        }else{

            $data->where( 'find_in_set(' . $id . ', expert_ids)');
        }
          return  $data->join('ah_case c','a.case_id = c.id')
            ->field('a.id,a.case_id,c.name,c.sex,c.age,a.meeting_at,c.illness_info,a.meeting_id,a.meeting_num,a.is_end,c.case_imgs')
              ->order('a.id desc')
            ->select()->each(function ($item){

                $item['case_imgs'] = json_decode($item['case_imgs'],true);
                $item['meeting_at'] = date('Y年m月d日',$item['meeting_at']);
            });
    }

    /**
     *
     * 向专家发送会议邀请
     */
    public static function meetingAdd($params){

        Db::startTrans();
        $res2 = TengXunModel::createMeeting($params['user_id'],$params['meeting_at']);
        if ($res2){

            $meeting = [

                'user_id'=>$params['user_id'],
                'case_id'=>$params['case_id'],
                'meeting_id'=>$res2,
               // 'meeting_num'=>$res2['data']['meetingNum'],
                'expert_ids'=>$params['expert_ids'],
                'meeting_at'=>strtotime($params['meeting_at']),
            ];
        }else{

            Db::rollback();
            return false;
        }

        $res1 = self::insert($meeting);

        if ($res1 && $res2){

            Db::commit();
            return $res2;
        }else{

            Db::rollback();
            return false;
        }

    }

    /**
     *
     * 修改会议信息
     */
    public static function meetingEdit($where = [],$update =[]){

        return self::where($where)->update($update);
    }

    /**
     *
     * 会议详情
     */
    public static function meetingInfo($id,$user_id){

            $data =  self::alias('m')
                ->where(['m.id'=>$id])
                ->join('ah_case c','m.case_id = c.id')
                ->field('m.id,m.case_id,m.expert_ids,m.meeting_at,m.meeting_id,m.meeting_url,c.name,c.sex,c.age,c.case_imgs,c.inspect_imgs,c.illness_info')
                ->find();
            if ($data){

                #病例图片
                if ($data['case_imgs']){

                    $data['case_imgs'] = json_decode($data['case_imgs'],true);
                }

                #检测报告图片
                if ($data['inspect_imgs']){

                    $data['inspect_imgs'] = explode('|',$data['inspect_imgs']);
                }
                $data['meeting_at'] = date('Y-m-d H:i',$data['meeting_at']);
                if ($data['expert_ids']){

                    $data['expert_info'] = UserModel::whereIn('id',$data['expert_ids'])->field('id,nickname,head_img')->select();
                }else{

                    $data['expert_info'] = [];
                }

                #AI聊天记录

                $data['chat_content'] = QwenModel::contentList($user_id,$id);
            }

            return $data;
    }




}